// miniprogram/pages/usersList.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    showBottom: false, // 显示底部状态栏
    total: '', // 总用户数
    pageIndex: 1, // 当前页面数
    users: [], //用户数据汇总
    perPage: 10, //每页显示用户数

    recordTotal:0, // 总记录数
    recordPageIndex: 1, // 当前页面数
    records: [], //用户数据汇总
    perPageRecord: 10, //每页显示用户数
  },
  tabSelect(e) {
    // //console.log(e);
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  query_data_by_current_typeid:function(index){
    let id = Number(index)
    if (id == 0){
      //获取用户总数
      this.getUsersCount()
      //获取用户信息
      this.getUserInfoByIndex(this.data.pageIndex)
    } else if (id == 1){
      //获取兑换总数
      this.getExchangedCount()
      //获取兑换记录
      this.getExchangedByIndex(this.data.pageIndex)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let openid = options.openid
    this.setData({
      openid: openid
    })
    this.query_data_by_current_typeid(0)
    this.query_data_by_current_typeid(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.toggleBottomStatus(true)
    let users = this.data.users
    let total = this.data.total
    let index = this.data.pageIndex

    //如果不是尾页，则需要加载数据
    if (users.length < total) {
      this.getUserInfoByIndex(index + 1)
    }

  },
  toggleBottomStatus: function (bool) {
    this.setData({
      showBottom: bool
    })
  },
  //------------------------------------------获取用户总数
  getUsersCount: function () {
    let that = this
    db.collection('activity_users').count().then(res => {
      that.setData({
        total: res.total
      })
    })
  },
  //------------------------------------------根据页码获取用户信息
  getUserInfoByIndex: function (index) {
    let that = this

    let limit = this.data.perPage
    let skip = limit * (index - 1)
    let users = this.data.users
    //console.log('skip:', skip, 'limit:', limit, 'users:', users)
    db.collection('activity_users')
      .orderBy('current_likes', 'desc')
      .skip(skip) // 跳过结果集中的xx条
      .limit(limit) // 限制返回数量为xx条
      .get()
      .then(res => {
        //console.log(res)
        for (let i = 0; i < res.data.length; i++) {
          users.push(res.data[i])
        }

        that.setData({
          users: users,
          pageIndex: index
        })
        that.toggleBottomStatus(false)
      })
  },

  //------------------------------------------获取用户总数
  getExchangedCount: function () {
    let that = this
    db.collection('activity_exchange_record').count().then(res => {
      that.setData({
        recordTotal: res.total
      })
    })
  },
  //------------------------------------------根据页码获取兑换信息
  getExchangedByIndex: function (index) {
    let that = this
    let limit = this.data.perPageRecord
    let skip = limit * (index - 1)
    let records = this.data.records
    //console.log('skip:', skip, 'limit:', limit, 'records:', records)
    db.collection('activity_exchange_record')
      .skip(skip) // 跳过结果集中的xx条
      .limit(limit) // 限制返回数量为xx条
      .get()
      .then(res => {
        //console.log(res)
        for (let i = 0; i < res.data.length; i++) {
          records.push(res.data[i])
        }

        that.setData({
          records: records,
          recordPageIndex: index
        })
        that.toggleBottomStatus(false)
      })
  },
})